package net.gz01.wallet.web.controller.api.sdk.client.authForfadada;

import net.gz01.wallet.web.controller.api.sdk.client.common.FddClient;
import net.gz01.wallet.web.controller.api.sdk.util.crypt.FddEncryptTool;
import net.gz01.wallet.web.controller.api.sdk.util.http.HttpsUtil;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

public class FindCertInfo extends FddClient {
    public FindCertInfo(String appId, String secret, String version, String url) {
        super(appId, secret, version, url);
    }
    /**
     * 查询个人实名认证信息接口地址
     * @return
     */
    public String getFindPersonCertInfoUrl() {
        return super.getUrl() + "find_personCertInfo.api";
    }
    /**
     * 查询企业实名认证信息接口地址
     * @return
     */
    public String getFindCompanyCertInfoUrl() {
        return super.getUrl() + "find_companyCertInfo.api";
    }


    /**
     * 查询个人实名认证信息接口
     * @date: 2018年12月23日
     * @param verified_serialno 实名认证返回序列号
     * @param account_type 1:个人，2:企业
     * @return
     */
    public String invokeFindPersonCert(String verified_serialno,String account_type) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        try {
            String timeStamp = HttpsUtil.getTimeStamp();
            String msgDigest;
            // Base64(SHA1(appid+md5(timestamp)+SHA1(appsecret)))
            String sha1 = FddEncryptTool.sha1(super.getAppId()
                    + FddEncryptTool.md5Digest(timeStamp)
                    + FddEncryptTool.sha1(super.getSecret()
                    + verified_serialno));
            msgDigest = new String(FddEncryptTool.Base64Encode(sha1.getBytes()));
            params.add(new BasicNameValuePair("verified_serialno", verified_serialno));

            params.add(new BasicNameValuePair("app_id", super.getAppId()));
            params.add(new BasicNameValuePair("timestamp", timeStamp));
            params.add(new BasicNameValuePair("v", super.getVersion()));
            params.add(new BasicNameValuePair("msg_digest", msgDigest));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        if(account_type.equals("1")){
            return HttpsUtil.doPost(this.getFindPersonCertInfoUrl(), params);
        }else {
            return HttpsUtil.doPost(this.getFindCompanyCertInfoUrl(), params);
        }
    }
}
